﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SAP.Middleware.Connector;

namespace Logistics.DataAccess
{
    public class LoginDao
    {

        /// <summary>
        /// 检测是否可以连接到oracle
        /// </summary>
        /// <returns></returns>
        /// 
        string g_table = "";

        public bool PinConnection()
        {
            return DBHelper.PinConnection();
        }


        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="workshop"></param>
        /// <param name="process"></param>
        /// <param name="password"></param>
        /// <returns></returns>

        public object tc_owi001(string l_tc_owr009)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("select distinct tc_owi001 from tc_owi_file where tc_owi008='{0}' ", l_tc_owr009);
            object l_tc_owi001 = DBHelper.GetSingle(sb.ToString());
            return l_tc_owi001;
        }

        public DataTable GetProductionInfo(string l_tc_owi001, string l_date)
        {

            string l_table = "";
            int l_cn = 0;
            while (true)
            {
                l_table = "sap_src" + DateTime.Now.ToString("yyMMddhhmmss");        // 08:05:57
                StringBuilder sel_sb_f = new StringBuilder();
                sel_sb_f.AppendFormat(" select count(1)  from user_tables where table_name=upper('{0}') ", l_table);
                l_cn = Convert.ToInt16(DBHelper.GetSingle(sel_sb_f.ToString()));
                if (l_cn == 0)
                {
                    break;
                }

            }

            g_table = l_table;

            StringBuilder sbSqlText = new StringBuilder();
            sbSqlText.Append(" CREATE GLOBAL TEMPORARY TABLE " + l_table.ToString());
            sbSqlText.Append(" (type  VARCHAR2(10),tc_owr001 VARCHAR2(10),tc_owr002 VARCHAR2(20),tc_owr003 VARCHAR2(20),tc_owr004 VARCHAR2(20),tc_owr005 VARCHAR2(20),tc_owr006 VARCHAR2(20),tc_owr007 VARCHAR2(20),tc_owr008 VARCHAR2(20),tc_owr009 VARCHAR2(20),tc_owr010 VARCHAR2(20),tc_owr016 VARCHAR2(300)) ");
            sbSqlText.Append(" ON COMMIT PRESERVE ROWS");
            DBHelper.ExecuteSql(sbSqlText.ToString());

           
            RfcDestination SapRfcDestination = RfcDestinationManager.GetDestination("DEV");
            RfcRepository SapRfcRepository = SapRfcDestination.Repository;

            //转储订单
            IRfcFunction myfun = SapRfcRepository.CreateFunction("ZMMRFC_POMIGO");
            //传入结构INPUT  
            myfun.SetValue("ZPOINTF", "PSUB01");
            myfun.SetValue("CALL_TYPE", "Q");
            myfun.Invoke(SapRfcDestination);
            IRfcTable IrfTable = myfun.GetTable("IT_ZTMM009K");
            //循环把IRfcTable里面的数据放入Table里面，因为类型不同，不可直接使用。             
            for (int i = 0; i < IrfTable.Count; i++)
            {
                IrfTable.CurrentIndex = i;
                string l_bedat = IrfTable.GetString("Bedat");
                string l_Lifnr = Convert.ToString(tc_owi001(IrfTable.GetString("Lifnr")));
              //  if ((l_bedat == l_date) && (l_Lifnr == l_tc_owi001))
             //   {

                    string l_type = "转储订单";
                    string l_tc_owr001 = IrfTable.GetString("ZPOINTF");
                    if (l_tc_owr001 == "PSUB01") { l_tc_owr001 = "14"; }
                    if (l_tc_owr001 == "PSUB02") { l_tc_owr001 = "24"; }
                    string l_tc_owr002 = IrfTable.GetString("EBELN");
                    string l_tc_owr003 = IrfTable.GetString("Bukrs");
                    string l_tc_owr004 = "";
                    string l_tc_owr005 = IrfTable.GetString("Bukrs");
                    string l_tc_owr006 = l_bedat;
                    string l_tc_owr007 = "";
                    string l_tc_owr008 = "";
                    string l_tc_owr009 = l_Lifnr;
                    string l_tc_owr010 = IrfTable.GetString("Lifn2");
                    string l_tc_owr016 = IrfTable.GetString("Zzmem");

                   StringBuilder inssbSqlText = new StringBuilder();
                   inssbSqlText.Append(" insert into " + l_table.ToString() + " VALUES ");
                   inssbSqlText.AppendFormat("('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", l_type, l_tc_owr001, l_tc_owr002, l_tc_owr003, l_tc_owr004, l_tc_owr005, l_tc_owr006, l_tc_owr007, l_tc_owr008, l_tc_owr009, l_tc_owr010, l_tc_owr016);
                   DBHelper.ExecuteSql(inssbSqlText.ToString());
             //   }
            }


            StringBuilder selsb = new StringBuilder();
            selsb.Append(" select * from " + l_table.ToString());
            DataSet ds = DBHelper.Query(selsb.ToString());


            StringBuilder del_sb = new StringBuilder();
            del_sb.Append(" truncate table " + l_table.ToString());
            DBHelper.ExecuteSql(del_sb.ToString());

            del_sb = new StringBuilder();
            del_sb.Append(" DROP TABLE " + l_table.ToString());
            DBHelper.ExecuteSql(del_sb.ToString());

            return ds.Tables.Count > 0 ? ds.Tables[0] : null;


        }

    }
}
